package com.jt.web;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.jt.vo.SysResult;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * 底层是aop的具体实现
 * @RestControllerAdvice 通知
 * @author dc
 * @Classname GlobalExceptionHandler
 * @Date 2020/2/10 16:08
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
    /**
     * @ExceptionHandler 描述的方法为异常处理方法,
     * 注解内部定义的异常类型为此方法可以处理的异常类型
     */
    @ExceptionHandler(RuntimeException.class)
    public Object doHandleRuntimeException(RuntimeException e, HttpServletRequest httpServletRequest) {
        e.printStackTrace();
        //封装异常信息
        String callback=httpServletRequest.getParameter("callback");
        if(StringUtils.isEmpty(callback)){
            return SysResult.fail(e);
        }else {
            return new JSONPObject(callback,SysResult.fail());
        }

    }



}
